package com.ruoyi.activity.controller.admin;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.ruoyi.activity.req.AccountReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.activity.domain.Account;
import com.ruoyi.activity.service.IAccountService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 账号管理
 *
 * @author wxc
 * @date 2024-05-29
 */
@RestController
@RequestMapping("/admin/account")
public class AccountController extends BaseController
{
    @Autowired
    private IAccountService accountService;

    /**
     * 查询账号列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Account account)
    {
        startPage();
        List<Account> list = accountService.selectAccountList(account);
        return getDataTable(list);
    }

    /**
     * 下载账号列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, Account account)
    {
        List<Account> list = accountService.selectAccountList(account);
        ExcelUtil<Account> util = new ExcelUtil<Account>(Account.class);
        util.exportExcel(response, list, "账号数据");
    }

    /**
     * 获取账号详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(accountService.selectAccountById(id));
    }

    /**
     * 新增账号
     */
    @PostMapping
    public AjaxResult add(@RequestBody@Valid AccountReq req)
    {
        return toAjax(accountService.insertAccount(req));
    }

    /**
     * 修改账号
     */
    @PutMapping
    public AjaxResult edit(@RequestBody@Valid AccountReq req)
    {
        return toAjax(accountService.updateAccount(req));
    }

    /**
     * 删除账号
     */
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(accountService.deleteAccountByIds(ids));
    }
    /**
     * 上传账号
     */
    @PostMapping("/upload")
    public AjaxResult upload(@RequestPart("file")MultipartFile file) throws IOException {
        ExcelUtil<Account> util = new ExcelUtil<Account>(Account.class);
        List<Account> accountList = util.importExcel(file.getInputStream());
        return toAjax(accountService.insertAccountList(accountList));
    }

}
